import 'dart:convert';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../service/service_method.dart';
import '../commant/Swiper.dart';
import '../commant/homeTopNavigator.dart';
import '../commant/adBanner.dart';
import '../tempData/tempdata.dart';
import '../commant/recomend.dart';
import '../router_total.dart';


class HomePage extends StatefulWidget  {
  @override
  _HomePageState createState() => _HomePageState();
}

//保持页面需要设置AutomaticKeepAliveClientMixin 混入这个
/*
使用的页面必须是StatefulWidget,如果是StatelessWidget是没办法办法使用的。
其实只有两个前置组件才能保持页面状态：PageView和IndexedStack。
重写wantKeepAlive方法，如果不重写也是实现不了的。
*/
class _HomePageState extends State<HomePage> {


  TextEditingController typeController = TextEditingController();
  String showText = '欢迎你来到美好人间';


  @override
  void initState() {
    getHomePageContent().then((value){
      setState(() {
        showText=value.toString();
      });
    });
    super.initState();


  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text('首页'),
        ),
        body: SingleChildScrollView(
          child: Container(
            child: Column(
              children: <Widget>[
                SwiperDiy(swiperList,(index){
                  print('点击了轮播图');
                  print("点击了第$index--张轮播图");
                  Navigator.pushNamed(context, '/detail');
                }),
                Text(
                  '我是轮播图的信息',
                  textAlign: TextAlign.left,
                ),
                TopNavigator(navigatorList),
                AdBanner(advertesPicture:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1587812779763&di=c751dcb3261f6b892d469adab8ecfbd4&imgtype=0&src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Farchive%2Fa5804a2d5011fc763a8cd8e6e061a3376898ff12.jpg',phoneNumber: '18668460187',),
                Recommend(recomendList: recomendList,)
              ],
            ),

          ),
        )
//      body: FutureBuilder(//动态的组件 可以实现setState的功能
//          future: getHomePageContent(),
//          builder:(context, snapshot){
//            if(snapshot.hasData) {
//              var data = json.decode(snapshot.data.toString());
//              List<Map> swiperDateList = (data['data']['slider'] as List).cast();
//              return Column(
//                children: <Widget>[
//                  SwiperDiy(
//                    swiperDateList: swiperDateList,
//                  )
//                ],
//              );
//            } else {
//              return Center(
//                child: Text('没有轮播数据。。。'),
//              );
//            }
//          }
//      ),

//        body: SingleChildScrollView( //这个组件可以解决键盘出现时 页面高度不够出现的条纹问题
//          child: Container(
//            child: Column(
//              children: <Widget>[
//                TextField(
//                  controller: typeController,
//                  decoration: InputDecoration(
//                    contentPadding: EdgeInsets.all(10.0),
//                    labelText: '美女类型',
//                    helperText: '请输入你要选择的类型',
//                  ),
//                  autofocus: false,
//                ),
//                RaisedButton(
//                  onPressed: this._choiceAction,
//                  child: Text('选择完毕'),
//                ),
//                Text(
//                  showText,
//                  overflow: TextOverflow.ellipsis,
//                  maxLines: 1,
//                )
//              ],
//            ),
//          ),
//        ),

      ),
    );
  }


  //测试方法
  void _choiceAction() {
    print('开始选择你喜欢的类型。。。。');
    if (typeController.text.toString() == '') {
      showDialog(
          context: context,
          builder: (context) =>
              AlertDialog(
                title: Text('选择的类型不能为空'),
              )
      );
    } else {
      getHttp(typeController.text.toString()).then((value) {
        showText = value['data'];
      });
    }
  }

  Future getHttp(String TypeText) async {
    try {
      Response response;
      var data = {'name': TypeText};
      response = await Dio().get(
          "https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian",
          queryParameters: data
      );
      return response.data;
    } catch (e) {
      print(e);
    }
  }




//  Future getHttp2(String TypeText) async{
//    try{
//      Response response;
//      var data={'name':TypeText};
//      Dio dio= Dio();
//      dio.options.headers=httpHeaders;
//      response = await Dio().get("https://time.geekbang.org/?m=0&d=3&c=3&sort=1&order=sort");
//      print(response);
//      return response.data;
//    }catch(e){
//      print(e);
//    }
//  }
//}
}




